package com.shirley.netty.udp.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

public class UdpServer {

    public static final int SERVER_PORT = 8822;

    public static void main(String[] args) {
        EventLoopGroup bossLoopGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(bossLoopGroup)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.AUTO_CLOSE, true)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(new UdpServerChannelInit());

            ChannelFuture channelFuture = bootstrap.bind(SERVER_PORT).sync();
            System.out.println("UDP server started.");
            channelFuture.channel().closeFuture().sync();

            Channel ch = channelFuture.channel();
            ch.writeAndFlush("Msg from server");
        } catch (Exception e) {
            bossLoopGroup.shutdownGracefully();
        }
    }
}
